package heven.holt.wan.android

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import heven.holt.library.base.BaseFragment
import heven.holt.library.extensions.getResourceIdByArray
import heven.holt.library.loading.LoadingStateView
import heven.holt.library.loading.ViewType
import heven.holt.library.loading.delegate.ErrorViewDelegate
import heven.holt.library.loading.extensions.setLoadingStateCommonDecorHeader
import heven.holt.wan.android.adapter.HomeViewPagerAdapter
import heven.holt.wan.android.databinding.FragmentMainBinding
import heven.holt.wan.android.databinding.ItemTabMainBinding
import heven.holt.wan.android.listener.HomeTabSelectedListener
import heven.holt.wan.android.loading.delegate.SearchHeaderViewDelegate
import heven.holt.wan.android.viewmodel.MainViewModel
import javax.inject.Inject

@AndroidEntryPoint
class MainFragment : BaseFragment<FragmentMainBinding>() {
    private val mainViewModel: MainViewModel by activityViewModels()

    private val tabTexts by lazy { resources.getStringArray(R.array.home_tab_name) }

    @Inject
    lateinit var searchHeaderViewDelegate: SearchHeaderViewDelegate

    private lateinit var loadingStateView: LoadingStateView

    override fun bindViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentMainBinding = FragmentMainBinding.inflate(inflater, container, false)

    override fun createDecorationView(): View {
        loadingStateView = binding.root.setLoadingStateCommonDecorHeader(searchHeaderViewDelegate)
        loadingStateView.register(ViewType.ERROR, ErrorViewDelegate {
            childFragmentManager.fragments.forEach {
                if (it is LoadingStateView.OnReloadListener) {
                    it.onReload()
                }
            }
        })
        return loadingStateView.decorView
    }

    fun showLoadingView() {
        loadingStateView.showLoadingView()
    }

    fun showContentView() {
        loadingStateView.showContentView()
    }

    fun showErrorView() {
        loadingStateView.showErrorView()
    }

    override fun initView() {
        initTabLayoutViewPager()
    }

    private fun initTabLayoutViewPager() {
        binding.viewPager.apply {
            adapter = HomeViewPagerAdapter(this@MainFragment)
            offscreenPageLimit = 5
            isUserInputEnabled = false
        }
        binding.tabLayout.addOnTabSelectedListener(HomeTabSelectedListener())
        TabLayoutMediator(binding.tabLayout, binding.viewPager, true, false) { tab, position ->
            getTab(position, tab)
        }.attach()
    }

    override fun initData() {
        viewLifecycleOwner.lifecycle.addObserver(searchHeaderViewDelegate)
        mainViewModel.hotKeys.observe(viewLifecycleOwner, {
            it?.let { it1 -> searchHeaderViewDelegate.bindHotKeys(it1) }
        })

        if (mainViewModel.hotKeys.value == null)
            mainViewModel.requestHotKeys()
    }

    private fun getTab(position: Int, tab: TabLayout.Tab) {
        val itemTabMainBinding =
            ItemTabMainBinding.inflate(LayoutInflater.from(binding.tabLayout.context))
        val id = resources.getResourceIdByArray(R.array.home_tab_icon, position)
        itemTabMainBinding.icon.setImageResource(id)
        itemTabMainBinding.icon.setColorFilter(
            ContextCompat.getColor(
                itemTabMainBinding.icon.context,
                R.color.gray_alpha
            )
        )
        itemTabMainBinding.name.text = tabTexts[position]
        itemTabMainBinding.name.setTextColor(
            ContextCompat.getColor(
                itemTabMainBinding.name.context,
                R.color.gray_alpha
            )
        )
        tab.customView = itemTabMainBinding.root
    }
}